import {set} from "lodash-es"
import {DEFAULT_STATUS_COLOR,DEFAULT_EDGE_LINE_STYLE} from "./../metadata"
import { IPackNifi } from "src/inter";
import {t} from "../../i18n/index"

export default {
    inherit: 'edge',
    zIndex:10,
    defaultLabel: {
        markup: [
            { tagName: 'rect', selector: 'body' },
            { tagName: 'text', selector: 'label', },
            { tagName: "text", selector: "quene" },
            { tagName: "text", selector: "byte" },
            { tagName: "rect", selector: "precent" },
        ],
        attrs: {
            label: {
                ref: "body",
                fill: '#333',
                fontWeight: "bold",
                fontSize: 14,
                refY: "6",
                refX: "6",
                textWrap:{ellipsis:true,breakWord:false,height: 20,width:88},
            },
            body: {
                width: 100,
                height: 40,
                xAlign: "middle",
                yAlign: "middle",
                rx: 8,
                fill: {
                    type: 'linearGradient',
                    stops: [
                        { offset: '0%', color: '#66CC00' },
                        { offset: '0%', color: '#fff' },
                    ],
                },
                stroke: '#ffa940',
                strokeWidth: 2,
                // refX: "50%",
            },
            quene: {
                ref: "body",
                text: "99",
                fill: "#666",
                yAlign: "top",
                fontSize: 14,
                fontWeight: "bold",
                refX: 6,
                textWrap:{ellipsis:true,height: 20,width:50}
            },
            byte: {
                ref: "quene",
                text: "(88 byte)",
                refX: "100%",
                refY: "100%",
                yAlign: "bottom",
                fontSize: 8,
                x: 4,
                refY2: "-3",
                fill: "#999"
            },
        },
        position: {
            distance: 0.5,
            options: {
                absoluteDistance: true,
                reverseDistance: true,
            },
        },
    },
    propHooks(metadata:any) {
        const { text, quene,byte,precent,status,bytePrecent } = metadata
        // label 属性比较特殊，不会出现在 metadata 中
        if(text) { 
            set(metadata,"labels/0/attrs/label/title".split('/'), text);
        }
        if(quene){set(metadata, "labels/0/attrs/quene/text".split('/'), quene);set(metadata, "labels/0/attrs/quene/title".split('/'), `排队记录数：${quene} :: 排队记录数百分比： ${precent}%`)}
        if(byte){set(metadata, "labels/0/attrs/byte/text".split('/'), byte);set(metadata, "labels/0/attrs/byte/title".split('/'), `排队字节数：${byte} :: 排队字节数百分比： ${bytePrecent}%`)}
        if(precent != undefined){
            let c1 = '#66CC00'
            let c2 = '#fff'
            if(precent >= 100){
              c1 = "#FF0033"
              c2 = "#FF6666"  
            }
            const v = {type: 'linearGradient',stops:[{ offset: '0%', color: c1 },{ offset: `${precent}%`, color: c2 }]};
            set(metadata, "labels/0/attrs/body/fill".split('/'), v);
            
        }
        if(status){
            const c:string = DEFAULT_STATUS_COLOR[status as IPackNifi.NIFI_CELL_STATUS] || '#fff';
            const s = DEFAULT_EDGE_LINE_STYLE[status as IPackNifi.NIFI_CELL_STATUS]
            set(metadata,"labels/0/attrs/body/stroke".split('/'),c)
            set(metadata,'attrs.line.stroke',c)
            set(metadata, "labels/0/attrs/body/title".split('/'), `当前状态：${status} `);
            if(s){
                for(const k of Object.keys(s)){
                    set(metadata,['attrs','line',k],s[k])
                }
            }
        }
        return metadata;
    }
}



